package cn.broadsense.biz.service.impl;

import cn.broadsense.biz.domain.Channel;
import cn.broadsense.biz.mapper.ChannelMapper;
import cn.broadsense.biz.service.ChannelService;
import cn.broadsense.biz.vo.ChannelVo;
import cn.broadsense.common.core.domain.R;
import cn.broadsense.common.exception.ServiceException;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.broadsense.biz.domain.ChannelGovernanceDetails;
import cn.broadsense.biz.service.ChannelGovernanceDetailsService;
import cn.broadsense.biz.mapper.ChannelGovernanceDetailsMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author bs
 * @description 针对表【t_channel_governance_details(数据治理明细)】的数据库操作Service实现
 * @createDate 2024-04-11 17:15:15
 */
@Service
public class ChannelGovernanceDetailsServiceImpl extends ServiceImpl<ChannelGovernanceDetailsMapper, ChannelGovernanceDetails>
        implements ChannelGovernanceDetailsService {

    @Resource
    private ChannelService channelService;

    @Override
    public Channel getRecDetails(Long id) {
        ChannelGovernanceDetails governanceDetails = this.getById(id);
        if (governanceDetails == null) {
            throw new ServiceException("数据不存在");
        }
        Channel details = channelService.getDetails(governanceDetails.getChannelId());
        if (details == null) {
            throw new ServiceException("当前点位信息已被删除");
        }
        Integer changeStatus = governanceDetails.getChangeStatus();
        if (1 == changeStatus && JSONUtil.isTypeJSON(governanceDetails.getChangeJson())) {
            ChannelVo channelvo = JSONObject.parseObject(governanceDetails.getChangeJson(), ChannelVo.class);
            BeanUtil.copyProperties(channelvo, details);
        }
        return details;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteId(Long id) {
        LambdaUpdateWrapper<ChannelGovernanceDetails> updateQuery = new LambdaUpdateWrapper<>();
        updateQuery.eq(ChannelGovernanceDetails::getId, id);
        updateQuery.set(ChannelGovernanceDetails::getChangeStatus, 2);
        boolean update = super.update(updateQuery);
        if (!update) {
            throw new ServiceException("数据不存在，删除失败");
        }
    }
}




